/**
 *
 * @author 
 *
 */
class ShouYeView extends eui.Component{
    
    private im_gz:eui.Image;
    private btn_gz:eui.Group;
    private btn_start:eui.Image;
    private btn_phb:eui.Image;
    private step:number = 0;
    
    private appid: any;
    private timestamp: any;
    private nonceStr: any;
    private signature: any;
    
    public audioSoundChannel: egret.SoundChannel;
    private audio: egret.Sound = new egret.Sound();
    
    private static _instance: ShouYeView;
    public static getInstance(): ShouYeView {
        if(ShouYeView._instance == null) {
            ShouYeView._instance = new ShouYeView();
        }
        return ShouYeView._instance;
    }
    
	public constructor() {
    	  super();
        this.skinName = "resource/skins/ShouYeViewSkin.exml";
	}
	
    public createChildren() {
        super.createChildren();
        this.btn_gz.addEventListener(egret.TouchEvent.TOUCH_TAP,this.openView,this);
        this.btn_start.addEventListener(egret.TouchEvent.TOUCH_TAP,this.openView,this);
        this.btn_phb.addEventListener(egret.TouchEvent.TOUCH_TAP,this.openView,this);
        this.init();
        this.fenx();
    }
    
    public initM(){
        if(this.audioSoundChannel) { this.audioSoundChannel.stop(); }
        this.audio = RES.getRes("music2_mp3");
        this.audioSoundChannel = this.audio.play(0,1000000);
    }
    
    public stopm(){
        this.audioSoundChannel.stop(); 
    }
    
    private openView(ev:egret.TouchEvent){
        switch(ev.target){
            case this.btn_gz:
                var gz:GuiZheView = new GuiZheView();
                Main.getInstance().addChild(gz);
                this.stopm();
            break;
            case this.btn_start:
                if(Main.getInstance().contains(ShouYeView.getInstance())) {
                    ShouYeView.getInstance().parent.removeChild(ShouYeView.getInstance());
                }
                Main.getInstance().addChild(GameScene.getInstance());
                this.stopm();
            break;
            case this.btn_phb:
                if(Main.getInstance().contains(ShouYeView.getInstance())) {
                    ShouYeView.getInstance().parent.removeChild(ShouYeView.getInstance());
                }
                Main.getInstance().addChild(PHBView.getInstance());
                this.sendForPHB();
                this.stopm();
            break;
        }
    }
    
    private sendForPHB() {
        //给后端发请求
//        var nickName = egret.localStorage.getItem("nickname");
        var openid = egret.localStorage.getItem("openid");
        var request1 = new egret.HttpRequest();
        request1.responseType = egret.HttpResponseType.TEXT;
        request1.open("http://ztetj7.weixinzjit.com/ZTE/index.php/User/info?" + "openid=" + openid ,egret.HttpMethod.GET);
        request1.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
        request1.send();
        request1.addEventListener(egret.Event.COMPLETE,this.onGetComplete3,this);
        request1.addEventListener(egret.IOErrorEvent.IO_ERROR,this.onGetIOError3,this);
        request1.addEventListener(egret.ProgressEvent.PROGRESS,this.onGetProgress3,this);
    }

    private onGetComplete3(event: egret.Event): void {
        var request = <egret.HttpRequest>event.currentTarget;
        var response = request.response;
        var ob = JSON.parse(response);
        PHBView.getInstance().myPM.text = ob.rank+"位";
        PHBView.getInstance().setData(ob.data);
    }
    private onGetIOError3(event: egret.IOErrorEvent): void {
        
    }
    private onGetProgress3(event: egret.ProgressEvent): void {
//        alert("get error : " + event);
    }
    
    private init(){
        egret.Tween.get(this,{ loop:true }).to({ t: Math.PI * 2 },800);
        if(this.audioSoundChannel) { this.audioSoundChannel.stop(); }
        this.audio = RES.getRes("music2_mp3");
        this.audioSoundChannel = this.audio.play(0,1000000);
    } 
    private _t: number = 0;
    get t(): number { return this._t; }
    set t(value: number) {
        this._t = value;
        this.btn_start.scaleX = Math.sin(value) * 0.05 + 1;
        this.btn_start.scaleY = Math.sin(value - Math.PI / 2) * 0.05 + 1;
        this.btn_phb.scaleX = Math.sin(value) * 0.05 + 1;
        this.btn_phb.scaleY = Math.sin(value - Math.PI / 2) * 0.05 + 1;
        this.im_gz.scaleX = Math.sin(value) * 0.05 + 1;
        this.im_gz.scaleY = Math.sin(value - Math.PI / 2) * 0.05 + 1;
    }

    //获取APPID  与url
    private fenx() {
        var url:string = window.location.href;
        var params = "";
        var request = new egret.HttpRequest();
        request.responseType = egret.HttpResponseType.TEXT;
        request.open("http://ztetj7.weixinzjit.com/ZTE/Api/getSign",egret.HttpMethod.POST);
        request.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
        request.send(params);
        request.addEventListener(egret.Event.COMPLETE,this.onGetComplete1,this);
        request.addEventListener(egret.IOErrorEvent.IO_ERROR,this.onGetIOError1,this);
        request.addEventListener(egret.ProgressEvent.PROGRESS,this.onGetProgress1,this);
    }

    private onGetComplete1(event: egret.Event): void {
        var request = <egret.HttpRequest>event.currentTarget;
        var response = request.response;
        var object = JSON.parse(response);
        var appid = object.appid;
        this.appid = appid;
        var url = object.link;
        this.timestamp = parseInt(object.timestamp);
        console.log("this.timestamp ==========" + this.timestamp);
        this.nonceStr = object.noncestr;
        console.log("this.nonceStr ==========" + this.nonceStr );
        this.signature = object.signature;
        this.fenxiang1("参与中兴天机抢7挑战赛，赢取中兴天机7手机，快来参加吧！");
        // 具体解析参考上面的代码
    }
    private onGetIOError1(event: egret.IOErrorEvent): void {
        //        alert("get error : " + event);
    }
    private onGetProgress1(event: egret.ProgressEvent): void {

    }

    //微信分享借口
    public fenxiang1(title: any) {
        var bodyConfig: BodyConfig = new BodyConfig();
        bodyConfig.appId = this.appid;
        bodyConfig.debug = false;
        bodyConfig.timestamp = this.timestamp;
        bodyConfig.nonceStr = this.nonceStr;
        bodyConfig.signature = this.signature;
        bodyConfig.jsApiList = ['onMenuShareTimeline','onMenuShareAppMessage'];
        /// 通过config接口注入权限验证配置
        if(wx) {
            wx.config(bodyConfig);
            wx.ready(function() {
                /// 在这里调用微信相关功能的 API
                var bodyMenuShareTimeline = new BodyMenuShareTimeline();
                bodyMenuShareTimeline.title = "参与中兴天机抢7挑战赛，赢取中兴天机7手机，快来参加吧！";
                bodyMenuShareTimeline.link = 'http://ztetj7.weixinzjit.com';
                bodyMenuShareTimeline.imgUrl = "http://ztetj7.weixinzjit.com/resource/assets/fenx.png";
                wx.onMenuShareTimeline(bodyMenuShareTimeline);
                /// 在这里调用微信相关功能的 API
                var bodyMenuShareAppMessage = new BodyMenuShareAppMessage();
                bodyMenuShareAppMessage.desc = "全民庆奥运，万人免费享天机7";
                bodyMenuShareAppMessage.title = "参与中兴天机抢7挑战赛，赢取中兴天机7手机，快来参加吧！";
                bodyMenuShareAppMessage.link = 'http://ztetj7.weixinzjit.com';
                bodyMenuShareAppMessage.imgUrl = "http://ztetj7.weixinzjit.com/resource/assets/fenx.png";
                wx.onMenuShareAppMessage(bodyMenuShareAppMessage);
            });
        }
    }
    
}
